using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

namespace Gko.Utils
{
	public static class InvokeHelper
	{
		/// <summary>
		/// Invokes the method or constructor represented by the current instance, using the specified parameters.
		/// Throws InnerException of <see cref="T:System.Reflection.TargetInvocationException"/> without loosing the stack trace
		/// </summary>
		/// <param name="method">Method to ivoke</param>
		/// <param name="obj">The object on which to invoke the method or constructor. If a method is static, this argument is ignored. If a constructor is static, this argument must be null or an instance of the class that defines the constructor.</param>
		/// <param name="parameters">An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, parameters should be null.  If the method or constructor represented by this instance takes a ref parameter (ByRef in Visual Basic), no special attribute is required for that parameter in order to invoke the method or constructor using this function. Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is null. For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.</param>
		/// <returns>An object containing the return value of the invoked method, or null in the case of a constructor.</returns>
		/// <exception cref="T:System.Reflection.TargetException">The obj parameter is null and the method is not static.  -or- The method is not declared or inherited by the class of obj. -or- A static constructor is invoked, and obj is neither null nor an instance of the class that declared the constructor.</exception>
		/// <exception cref="T:System.ArgumentException">The elements of the parameters array do not match the signature of the method or constructor reflected by this instance.</exception>
		/// <exception cref="T:System.Reflection.TargetParameterCountException">The parameters array does not have the correct number of arguments.</exception>
		/// <exception cref="T:System.MethodAccessException">The caller does not have permission to execute the constructor.</exception>
		/// <exception cref="T:System.InvalidOperationException">The type that declares the method is an open generic type. That is, the System.Type.ContainsGenericParameters property returns true for the declaring type.</exception>
		[DebuggerNonUserCode]
		[DebuggerStepThrough]
		private static object Invoke(MethodInfo method, Object obj, params Object[] parameters)
		{
			try {
				return method.Invoke(obj, parameters);
			} catch (TargetInvocationException te) {
				if (te.InnerException == null) {
					throw;
				}
				var innerException = te.InnerException;

				var savestack = Delegate.CreateDelegate(typeof(ThreadStart), innerException, "InternalPreserveStackTrace", false, false) as ThreadStart;
				if (savestack != null) {
					savestack();
				}
				throw innerException; // -- now we can re-throw without trashing the stack
			}
		}
	}
}
